package com.wthink.hzgj.copyFile.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.alibaba.fastjson.JSON
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.copyFile.callback.DirectoryIsEmpty
import com.wthink.hzgj.data.Result
import com.wthink.hzgj.data.domain.CameraFile
import com.wthink.hzgj.data.domain.FileDetail
import com.wthink.hzgj.service.DevService
import com.wthink.hzgj.util.`interface`.ProgrossCallBack
import com.wthink.hzgj.util.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CopyFileDirectoryViewModel : ViewModel(){

    val fileDetailList = MutableLiveData(emptyList<FileDetail>())

    var fileList = emptyList<FileDetail>()

    lateinit var progressCallBack : ProgrossCallBack

    fun setprogressCallBack(callBack: ProgrossCallBack) {
        progressCallBack = callBack
    }

    lateinit var directoryIsEmpty: DirectoryIsEmpty

    fun setdirectoryIsEmpty(callBack: DirectoryIsEmpty) {
        directoryIsEmpty = callBack
    }

    fun start(){
        waitCopyProcess()
    }

    fun getList():List<FileDetail>{
        return fileList
    }

    fun queryDetail(path:String):List<FileDetail> {
        viewModelScope.launch(Dispatchers.IO) {
            val queryCameraFile = DevService.queryCameraVideo(path)
            if (queryCameraFile is Result.Success) {
                val list = queryCameraFile.data.fileList
                fileDetailList.postValue(list)
                fileList = list
                withContext(Dispatchers.Main) {
                    if (fileList.size<=0) {
                        toast(BusApplication.busContext, "请选择文件夹")
                    }
                }
            }
        }
        return fileList
    }

    fun queryDetailCheck(path:String):List<FileDetail> {
        viewModelScope.launch(Dispatchers.IO) {
            val queryCameraFile = DevService.queryCameraVideo(path)
            if (queryCameraFile is Result.Success) {
                val list = queryCameraFile.data.fileList
                directoryIsEmpty.fileSize(list.size)
                //fileDetailList.postValue(fileList)
                fileList = list
                withContext(Dispatchers.Main) {
                    if (fileList.size<=0) {
                        toast(BusApplication.busContext, "请选择文件夹")
                    }
                }
            }
        }
        return fileList
    }

    fun copyFile(list: ArrayList<String>) {
        val toJSONString = JSON.toJSONString(list)
        Log.d("copyFile",toJSONString)
        viewModelScope.launch (Dispatchers.IO){
            val copyVideoFile = DevService.copyVideoFile(toJSONString)
            if (copyVideoFile is Result.Success) {
                withContext(Dispatchers.Main) {
                    toast(BusApplication.busContext, "正在向主机发送拷贝申请")                }
            }else if (copyVideoFile is Result.Error) {
                withContext(Dispatchers.Main) {
                    if (fileList.size<=0) {
                        toast(BusApplication.busContext, copyVideoFile.exception.toString())
                    }
                }
            }
        }
    }

    fun waitCopyProcess() {
        viewModelScope.launch(Dispatchers.IO) {
            DevService.listenProcess().collect {
                val process = it.process
                Log.d("process",process)
                withContext(Dispatchers.Main) {
                    progressCallBack.progress(process)                }
            }
        }
    }

}